<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- CSS only -->
    <link rel="stylesheet" href="../bootstrap.min.css" />
    <style>
      .red {
        color: red !important;
      }
      .search {
        width: 300px;
        margin: 20px 0;
      }
      .my-form {
        display: flex;
        margin: 20px 0;
      }
      .my-form input {
        flex: 1;
        margin-right: 20px;
      }
      .table > :not(:first-child) {
        border-top: none;
      }
      .contain {
        display: flex;
        padding: 10px;
      }
      .list-box {
        flex: 1;
        padding: 0 30px;
      }
      .list-box a {
        text-decoration: none;
      }
      .echarts-box {
        width: 600px;
        height: 400px;
        padding: 30px;
        margin: 0 auto;
        border: 1px solid #ccc;
      }
      tfoot {
        font-weight: bold;
      }
      @media screen and (max-width: 1000px) {
        .contain {
          flex-wrap: wrap;
        }
        .list-box {
          width: 100%;
        }
        .echarts-box {
          margin-top: 30px;
        }
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="contain">
        <!-- 左侧列表 -->
        <div class="list-box">
          <!-- 添加资产 -->
          <form class="my-form">
            <input
              type="text"
              class="form-control"
              placeholder="消费名称"
              v-model.trim="name"
            />
            <input
              type="text"
              class="form-control"
              placeholder="消费价格"
              v-model.trim.number="price"
            />
            <button type="button" class="btn btn-primary" @click="add">
              添加账单
            </button>
          </form>

          <table class="table table-hover">
            <thead>
              <tr>
                <th>编号</th>
                <th>消费名称</th>
                <th>消费价格</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(v, i) of list" :key="v.id">
                <td>{{ i + 1 }}</td>
                <td>{{ v.name }}</td>
                <td :class="{ red: v.price > 100 }">
                  {{ v.price.toFixed(2) }}
                </td>
                <td><a href="javascript:;" @click="del(v.id)">删除</a></td>
              </tr>
            </tbody>
            <tfoot>
              <tr>
                <td colspan="4">消费总计： {{ total }}</td>
              </tr>
            </tfoot>
          </table>
        </div>

        <!-- 右侧图表 -->
        <div class="echarts-box" id="main"></div>
      </div>
    </div>
    <script src="../echarts.min.js"></script>
    <script src="../vue.js"></script>
    <script src="../axios.js"></script>
    <script>
      /**
       * 接口文档地址：
       * https://www.apifox.cn/apidoc/shared-24459455-ebb1-4fdc-8df8-0aff8dc317a8/api-53371058
       *
       * 功能需求：
       * 1. 基本渲染
       * 2. 添加功能
       * 3. 删除功能
       * 4. 饼图渲染
       */
      const app = new Vue({
        el: '#app',
        data: {
          name: '',
          price: '',
          list: [],
        },
        computed: {
          total() {
            return this.list.reduce((sum, v) => sum + v.price, 0).toFixed(2);
          },
        },
        methods: {
          async del(id) {
            await axios({
              method: 'delete',
              url: 'https://applet-base-api-t.itheima.net/bill/' + id,
            });

            // 请求成功

            // 重新渲染
            await this.getList();
          },
          // 获取列表
          async getList() {
            const res = await axios({
              method: 'get',
              url: 'https://applet-base-api-t.itheima.net/bill',
              params: {
                creator: '小黑',
              },
            });
            // console.log(res, 'res');
            this.list = res.data.data;

            // 设置图表数据，渲染图表
            this.myChart.setOption({
              series: [
                {
                  data: this.list.map(v => ({ value: v.price, name: v.name })),
                },
              ],
            });
          },
          async add() {
            // this.list.push({
            //   id: Date.now(),
            //   name: this.name,
            //   price: this.price,
            //   creator: '小黑',
            // });

            await axios({
              method: 'post',
              url: 'https://applet-base-api-t.itheima.net/bill',
              data: {
                creator: '小黑',
                name: this.name,
                price: this.price,
              },
            });

            // 只要能够走到这里，说明添加请求成功

            this.name = '';
            this.price = '';

            // 需要重新加载最新的列表数据，进行渲染
            await this.getList();
          },
        },
        async created() {
          await this.getList();

          // 如果后面代码，需要等待列表获取完成后，才能继续，就需要加上 await 等待
        },
        mounted() {
          // 初始化图表实例
          this.myChart = echarts.init(document.querySelector('#main'));

          // 设置配置项
          this.myChart.setOption({
            // 大标题
            title: {
              text: '消费账单列表',
              left: 'center',
            },
            // 提示
            tooltip: {
              trigger: 'item',
            },
            // 图例
            legend: {
              orient: 'vertical',
              left: 'left',
            },
            // 系列数据配置
            series: [
              {
                name: '账单项',
                type: 'pie',
                radius: '50%', // 半径
                data: [
                  // { value: 1048, name: 'Search Engine' },
                  // { value: 735, name: 'Direct' },
                  // { value: 580, name: 'Email' },
                  // { value: 484, name: 'Union Ads' },
                  // { value: 300, name: 'Video Ads' },
                ],
                // 样式配置
                emphasis: {
                  itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)',
                  },
                },
              },
            ],
          });
        },
      });
    </script>
  </body>
</html>
